<?php namespace App\Http\Controllers\admin\admins;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use App\Models\Admin;
use Redirect;
use Session;
class AdminUserController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		
		if(Session::has('admin')){
           return view("admin.admins.index")->withIsAdminlist(true); 
		}else{
           return Redirect::to('admin/dologin');
		}      
    }
	public function adminList()
	{

		$current = (int)Input::get("current");
		$rowCount = (int)Input::get("rowCount");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";

		$keywords = Input::get('searchPhrase');
        $model= new Admin;
		if($keywords){
			$model = $model->where("account", "like", "%".$keywords."%");
			$count = $model->count();
			$data = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get()->toArray();
		}else{
			$count = $model->count();
			$data = $model->skip(($current - 1)*$rowCount)->take($rowCount)->get()->toArray();
		}

		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $data,
			"total" => $count
		);
	}



	public function addAdminUser()
	{
		$account = Input::get("account");
		$password = Input::get("password");
		$model = Admin::where("account", $account);
		if($model->count()){
			return self::error(self::ACCOUNT_IS_EXIST);
		}else{
			$model = new Admin;
			$model->account = $account;
			$model->password = $password;
			$model->save();
			return $this->json([]);
		}
	}

	public function deleteAdminUser()
	{
		$aid = Input::get("aid");
		$model = Admin::where("id", $aid);
		if($model->count()){
			$model->delete();
			return $this->json([]);
		}else{
			return;
		}
	}
      
    public function getAdmins()
    {   
    	$aid = Input::get("aid");
    	$model = Admin::where("id",$aid);
    	if($model->count()){
    		$data = $model->get()->toArray();
    		return $this->json(["data" => $data]);
    	}else{
    		return;
    	}

    }

      public function updateAdmins(){
		$password = Input::get("password");
        $account = Input::get("account");
        $model = Admin::where("account",$account);
        if($model->count()){
        	 $data=$model->update(["password"=>$password]);
        }else{
        	return $this->json([]);
        }
	}   

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
